import { async } from 'q';
<!--
 * @Author: wangding
 * @Date: 2020-11-09 16:47:32
 * @LastEditTime: 2021-01-26 15:47:54
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \solaxcloud\src\views\device\sn\add.vue
-->
<template>
    <div>
        <el-dialog
        :title="$t('add')"
        :visible="newVisible"
        width="50%"
        @close="()=>{$store.commit('setNewVisible',false)}"
        :close-on-click-modal='false'
        >
                <el-row :gutter="20">
                    <el-col :span="17" :offset="3">
                        <el-form ref="form" :model="form" label-width="160px" >
                            <el-form-item :label="$t('sn.snAdd.allotCount')" prop="snCount" :rules="[{ required: true, message: $t('index_js_notNull')}]">
                                <el-input v-model="form.snCount" :placeholder="$t('pleaseInput')"></el-input>
                            </el-form-item>
                            <el-form-item :label="$t('devWifi.deviceType')" prop="deviceType" :rules="[{ required: true, message: $t('index_js_notNull')}]">
                                <el-select v-model="form.deviceType" :placeholder="$t('pleaseSelect')">
                                    <el-option
                                    v-for="item in textDataList.WIFI_DEVICE_TYPE"
                                    :key="item.value"
                                    :label="item.code"
                                    :value="item.value">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item :label="$t('firm.sn.firmno')" prop="firmNo" :rules="[{ required: true, message: $t('index_js_notNull')}]">
                                <el-select v-model="form.firmNo" :placeholder="$t('pleaseSelect')">
                                    <el-option
                                    v-for="item in textDataList.frimNo"
                                    :key="item.value"
                                    :label="item.code"
                                    :value="item.value">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item :label="$t('firm.sn.affiliation')" prop="firmId" :rules="[{ required: true, message: $t('index_js_notNull')}]">
                                <el-select v-model="form.firmId" :placeholder="$t('pleaseSelect')">
                                    <el-option
                                    v-for="item in firmName"
                                    :key="item.sid"
                                    :label="item.firmName"
                                    :value="item.sid">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item :label="$t('version.versionAdd.partNumber')" prop="partNo" :rules="[{ required: false, message: $t('index_js_notNull')}]">
                                <el-select v-model="form.partNo" :placeholder="$t('pleaseSelect')">
                                    <el-option
                                    v-for="item in textDataList.WIFI_PART_NUM"
                                    :key="item.code"
                                    :label="item.code"
                                    :value="item.code">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item :label="$t('snAddAsyncFlag')" prop="isAsyn" :rules="[{ required: true, message: $t('index_js_notNull')}]">
                                <el-radio-group v-model="form.isAsyn">
                                    <el-radio :label="0">{{$t('label.NO')}}</el-radio>
                                    <el-radio :label="1">{{$t('label.YES')}}</el-radio>
                                </el-radio-group>
                            </el-form-item>
                        </el-form>
                    </el-col>
                </el-row>
            <span slot="footer" class="dialog-footer">
                <el-button @click="()=>{$store.commit('setNewVisible',false)}">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="save('form')">{{$t('sure')}}</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    components: {

    },
    props: {
        firmName:{
            type:Array,
            default:()=>{return []}
        }
    },
    data() {
        return {
            form:{
                snCount:'',
                deviceType:'',
                firmNo:'',
                firmId:'',
                partNo:'',
                isAsyn:0,
            },
            scopeDataList:[],
            textDataList:{}
        };
    },
    computed: {
        newVisible(){
            return this.$store.state.sysStore.newVisible
        }
    },
    watch: {
        firmName:{
            handler(nv,ov){
                if(nv){
                    this.form.firmId = nv[0].sid
                }
            }
        }
    },
    created() {

    },
    mounted() {
        this.getDictionaryBatch()
    },
    methods: {
        async getDictionaryBatch(){
            let res = await this.API.getDictionaryBatch({
                dictCodes:'frimNo,WIFI_DEVICE_TYPE,WIFI_PART_NUM',
                lang:localStorage.getItem('lan') || 'zh_CN'
            })
            this.textDataList = res || {}
        },
        async snAddEntity(){
            let res = await this.API.snAddEntity(Object.assign({},this.form,{firmName:this.comm.returnCode(this.firmName,this.form.firmId,'sid','firmName')}))
            if(this.comm.showMsg(res.success)){
                this.$store.commit('setNewVisible',false)
                this.$emit('uploadData',true)
            }
        },
        save(formName){
            this.$refs[formName].validate((valid) => {
              if (valid) {
                  this.snAddEntity()
              } else {
                console.log('error submit!!');
                return false;
              }
            });
        }
    },
};
</script>

<style  lang="less" scoped>

</style>
